package com.demo;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Utils {
    // 获取区块的SHA-256哈希值
    public static String SHA256(String... strings) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA-256");
            // 更新数据到MessageDigest
            for (String s : strings){
                md.update(s.getBytes());
            }
            // 获取SHA-256的哈希结果
            byte[] sha = md.digest();
            // 返回十六进制格式的哈希值
            return byte2hex(sha);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    // byte转十六进制字符串
    public static String byte2hex(byte[] b) {
        StringBuilder hs = new StringBuilder();
        for (int n = 0; n < b.length; n++) {
            String stmp = Integer.toHexString(b[n] & 0xFF);
            if (stmp.length() == 1) {
                hs.append("0").append(stmp);  // 如果是单字符，前面加0
            } else {
                hs.append(stmp);
            }
        }
        return hs.toString().toUpperCase();
    }

    // Transaction的深拷贝方法
    public static List<Transaction> deepCopyTransactions(List<Transaction> transactions) throws CloneNotSupportedException {
        List<Transaction> copy = new ArrayList<>();
        for (Transaction tx : transactions) {
            try {
                copy.add(tx.clone());
            } catch (CloneNotSupportedException e) {
                System.out.println("克隆交易失败: " + e.getMessage());
                throw e;
            }
        }
        return copy;
    }

    // 根据键获取值
    public static String getValueByKey(Map<String, String> list, String key) {
        for (Map.Entry<String, String> entry : list.entrySet()) {
            if (entry.getKey().equals(key)) {
                return entry.getValue();
            }
        }
        return null;
    }
    // 根据键重新赋值
    public static String setValue(List<Map.Entry<String, String>> list, String key,String value) {
        for (Map.Entry<String, String> entry : list) {
            if (entry.getKey().equals(key)) {
                entry.setValue(value);
            }
        }
        return null;
    }
    // 根据键删除键值对
    public static String removeEntryByKey(List<Map.Entry<String, String>> list, String key) {
        for (Map.Entry<String, String> entry : list) {
            if (entry.getKey().equals(key)) {
                list.remove(entry);
            }
        }
        return null;
    }

    // 从链中查找属于自己的交易入流
    public static List<Transaction> findMyTxIn(Chain myBlockChain,String myPubKey) {
        List<Block> blocks = myBlockChain.chain;
        List<Transaction> myTxIn = new ArrayList<>();
        for(Block block : blocks){
            List<Transaction> transactions = block.transactions;
            for (Transaction tx : transactions){
                if(tx.to.equals(myPubKey)){
//                    if(!tx.to.equals(tx.from)){// 此时的交易不属于零钱返还，需要加入myTxIn
                    myTxIn.add(tx);
//                    }
                }
            }
        }
        return myTxIn;
    }

    // 从链中查找属于自己的交易出流
    public static List<Transaction> findMyTxOut(Chain myBlockChain,String myPubKey) {
        List<Block> blocks = myBlockChain.chain;
        List<Transaction> myTxOut = new ArrayList<>();
        for(Block block : blocks){
            List<Transaction> transactions = block.transactions;
            for (Transaction tx : transactions){
                if(tx.from.equals(myPubKey)){
//                    if(!tx.from.equals(tx.to)){// 此时的交易不属于零钱返还，需要加入myTxIn
                    myTxOut.add(tx);
//                    }
                }
            }
        }
        return myTxOut;
    }
}
